package com.rgp.servicemap.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.common.UrlConstants;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.internalcommon.response.ServiceResponse;
import com.rgp.internalcommon.utils.JointUrlUtils;
import com.rgp.servicemap.service.ServiceFromMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-19 17:17
 */
@Service
public class ServiceFromMapServiceImpl implements ServiceFromMapService {

    @Value("${amap.key}")
    private String secretKey;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResponseResult addService(String name) {
        Map<String, String> map = new HashMap<>();
        map.put("key",secretKey);
        map.put("name",name);

        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.CREATE_SERVICE_URL, map);
        String restResponse = restTemplate.postForObject(url, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(restResponse);
        String errcode = jsonObject.getString("errcode");
        if (!errcode.equals("10000")){
            return ResponseResult.fail(CommonStatusEnum.CREATE_SERVICE_ERROR);
        }
        JSONObject data = jsonObject.getJSONObject("data");
        String sid = data.getString("sid");
        ServiceResponse serviceResponse = new ServiceResponse();
        serviceResponse.setSid(sid);
        return ResponseResult.success(serviceResponse);
    }

    @Override
    public ResponseResult deleteService(String sid) {
        Map<String, String> map = new HashMap<>();
        map.put("key",secretKey);
        map.put("sid",sid);

        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.DELETE_SERVICE_URL, map);
        String restResponse = restTemplate.postForObject(url, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(restResponse);
        String errcode = jsonObject.getString("errcode");
        if (!errcode.equals("10000")){
            return ResponseResult.fail(CommonStatusEnum.DELETE_SERVICE_ERROR);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult listService() {

        Map<String, String> map = new HashMap<>();
        map.put("key",secretKey);

        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.LIST_SERVICE_URL, map);
        String restResponse = restTemplate.getForObject(url,String.class);
        JSONObject jsonObject = JSONObject.parseObject(restResponse);
        String errcode = jsonObject.getString("errcode");
        if (!errcode.equals("10000")){
            return ResponseResult.fail(CommonStatusEnum.LIST_SERVICE_ERROR);
        }
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray results = data.getJSONArray("results");
        List<ServiceResponse> list = new ArrayList<>();
        JSONObject temp = null;
        for (int i = 0;i < results.size();i++){
            temp = results.getJSONObject(i);
            ServiceResponse serviceResponse = new ServiceResponse();
            serviceResponse.setSid(temp.getString("sid"));
            serviceResponse.setName(temp.getString("name"));
            list.add(serviceResponse);
        }
        ServiceResponse[] ts = list.toArray(new ServiceResponse[list.size()]);

        return ResponseResult.success(ts);
    }
}